shell script1 Min Read

Shell script to extract single file from a bunch of jar files

Gorav Singal

November 24, 2017

TL;DR

Use a shell script with unzip command and a for loop to extract a specific file from each jar in a directory, organizing output into per-jar folders.

Shell script to extract single file from a bunch of jar files

Problem Statement

I have a bunch of jar files lying in a folder. Lets name it /Users/labuser/jars

Each jar file has a manifest file in META_INF/MANIFEST.MF
I have to read each MANIFEST.MF file and get some information from it. So, instead of extracting whole jar, one by one is a tedius thing.

What I want

I want to extract that file in a folder whose name can be as of jar file.

Unzip command to extract a jar file

Following command can be used to unzip a complete jar file:

unzip <path of jar file>

If you want to unzip files to a particular folder

unzip <path of jar file> -d <path of target folder>

Note: if the target folder does not exists, it will be created

If you want to unzip only particular files

unzip <path of jar file> <path of file inside jar file>

If you want to unzip only particular files in a particular folder

unzip <path of jar file> <path of file inside jar file> -d <target folder>

Conclusion

That is all. Combine these, and you are done.

Shell script I used to solve this

for myfile in `ls /Users/labuser/jars` ; do unzip ../$myfile META-INF/MANIFEST.MF -d $myfile; done
Share

Related Posts

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Jira Rest APIs common usages

Jira Rest APIs common usages

This article shows some of common usages of JIRA rest apis. Note: This article…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…